Lesson 3: Operators and Expressions
Objectives:
- Understand and use arithmetic operators in JavaScript.
- Learn how to compare values using comparison operators.
- Explore logical operators and their applications.
- Master the conditional (ternary) operator for concise decision-making.
1. Arithmetic Operators
1.1 Addition (+)
Example:
let sum = 3 + 5;
1.2 Subtraction (-)
Example:
let difference = 10 - 7;
1.3 Multiplication (*)
Example:
let product = 4 * 6;
1.4 Division (/)
Example:
let quotient = 15 / 3;
1.5 Modulus (%)
Example:
let remainder = 17 % 5;
2. Comparison Operators
2.1 Equal to (==)
Example:
let isEqual = (10 == '10');
2.2 Strict Equal to (===)
Example:
let isStrictEqual = (10 === '10');
2.3 Not Equal to (!=)
Example:
let isNotEqual = (5 != '5');
2.4 Greater than (>)
Example:
let isGreaterThan = (15 > 10);
2.5 Less than (<)
Example:
let isLessThan = (3 < 8);
3. Logical Operators
3.1 AND (&&)
Example:
let isBothTrue = (true && true);
3.2 OR (||)
Example:
let isEitherTrue = (true || false);
3.3 NOT (!)
Example:
let isNotTrue = !true;
4. Conditional (Ternary) Operator
Syntax:
condition ? expression_if_true : expression_if_false;
Example:
let age = 20;
let canVote = (age >= 18) ? "Yes" : "No";